package com.aisino.api.model;

import lombok.Data;

import java.util.List;

public class DrugInventoryRequest extends DrugStandardRequest {
    private String uscc;
    private String data;
    private String sign;
    private List<DrugInventoryData> itemList;

    public String getUscc() {
        return uscc;
    }

    public void setUscc(String uscc) {
        this.uscc = uscc;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public List<DrugInventoryData> getItemList() {
        return itemList;
    }

    public void setItemList(List<DrugInventoryData> itemList) {
        this.itemList = itemList;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public static class DrugInventoryData {
        private String ypwybsm;
        private String yppzwh;
        private Double sl;
        private Integer pdlx;

        public String getYppzwh() {
            return yppzwh;
        }

        public void setYppzwh(String yppzwh) {
            this.yppzwh = yppzwh;
        }

        public String getYpwybsm() {
            return ypwybsm;
        }

        public void setYpwybsm(String ypwybsm) {
            this.ypwybsm = ypwybsm;
        }

        public Double getSl() {
            return sl;
        }

        public void setSl(Double sl) {
            this.sl = sl;
        }

        public Integer getPdlx() {
            return pdlx;
        }

        public void setPdlx(Integer pdlx) {
            this.pdlx = pdlx;
        }

        public DrugInventoryRequest.ValidationResult validate(int i) {
            if (ypwybsm == null || ypwybsm.trim().isEmpty()) {
                return new DrugInventoryRequest.ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第" + i + "条数据药品唯一标识码不能为空");
            }
            if (yppzwh == null || yppzwh.trim().isEmpty()) {
                return new DrugInventoryRequest.ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第" + i + "条数据yppzwh不能为空");
            }

            if (pdlx == null || pdlx < 1 || pdlx > 2) {
                return new DrugInventoryRequest.ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第" + i + "条数据入库类型必须为1(初始化), 2(定期盘点)");
            }

            if (sl == null || sl < 0) {
                return new DrugInventoryRequest.ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第" + i + "条数据库存数量必须大于0");
            }

            // 验证药品数量不能大于千万(10,000,000)
            if (sl > 10000000) {
                return new DrugInventoryRequest.ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第" + i + "条数据库存数量不能大于千万");
            }

            return null; // 验证通过
        }
    }

    /**
     * 验证请求参数是否合法
     *
     * @return 验证结果，null表示验证通过，否则返回错误信息
     */
    public DrugInventoryRequest.ValidationResult validate() {
        if (uscc == null || uscc.trim().isEmpty()) {
            return new DrugInventoryRequest.ValidationResult(ResultCode.PARAM_ERROR.getCode(), "统一社会信用代码不能为空");
        }

        if (itemList == null || itemList.isEmpty()) {
            return new DrugInventoryRequest.ValidationResult(ResultCode.PARAM_ERROR.getCode(), "数据体不能为空");
        }

        if (itemList.size() > 1000) {
            return new DrugInventoryRequest.ValidationResult(ResultCode.PARAM_ERROR.getCode(), "单次上传数据条数不能超过1000条");
        }

        int i = 1;
        for (DrugInventoryRequest.DrugInventoryData item : itemList) {
            DrugInventoryRequest.ValidationResult itemValidationResult = item.validate(i);
            i++;
            if (itemValidationResult != null) {
                return itemValidationResult;
            }
        }

        return null; // 验证通过
    }

    /**
     * 验证结果类
     */
    public static class ValidationResult {
        private int code;
        private String message;

        public ValidationResult(int code, String message) {
            this.code = code;
            this.message = message;
        }

        public int getCode() {
            return code;
        }

        public String getMessage() {
            return message;
        }
    }
}